/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/<a href="https://www.kaaction.com">KAA</a>"><a href="https://www.kaaction.com">KAA</a></a> All rights reserved.
 */
package com.kaaction.modules.ka.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.ka.entity.MaterialDetails;
import com.kaaction.modules.ka.service.MaterialDetailsService;

/**
 * 物料明细管理Controller
 * @author huanghaiming
 * @version 2017-04-26
 */
@Controller
@RequestMapping(value = "${adminPath}/ka/materialDetails")
public class MaterialDetailsController extends BaseController {

	@Autowired
	private MaterialDetailsService materialDetailsService;
	
	@ModelAttribute
	public MaterialDetails get(@RequestParam(required=false) String id) {
		MaterialDetails entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = materialDetailsService.get(id);
		}
		if (entity == null){
			entity = new MaterialDetails();
		}
		return entity;
	}
	
	@RequiresPermissions("ka:materialDetails:view")
	@RequestMapping(value = {"list", ""})
	public String list(MaterialDetails materialDetails, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MaterialDetails> page = materialDetailsService.findPage(new Page<MaterialDetails>(request, response), materialDetails); 
		model.addAttribute("page", page);
		return "modules/ka/materialDetailsList";
	}

	@RequiresPermissions("ka:materialDetails:view")
	@RequestMapping(value = "form")
	public String form(MaterialDetails materialDetails, Model model) {
		model.addAttribute("materialDetails", materialDetails);
		return "modules/ka/materialDetailsForm";
	}

	@RequiresPermissions("ka:materialDetails:edit")
	@RequestMapping(value = "save")
	public String save(MaterialDetails materialDetails, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, materialDetails)){
			return form(materialDetails, model);
		}
		
		int cnt = materialDetailsService.getEqual(materialDetails);
		if(cnt>0){
			model.addAttribute("message", "该供物料明细已存在");
			return form(materialDetails, model);
		}
		
		materialDetailsService.save(materialDetails);
		addMessage(redirectAttributes, "保存物料明细管理成功");
		return "redirect:"+Global.getAdminPath()+"/ka/materialDetails/?repage";
	}
	
	@RequiresPermissions("ka:materialDetails:delete")
	@RequestMapping(value = "delete")
	public String delete(MaterialDetails materialDetails, RedirectAttributes redirectAttributes) {
		materialDetailsService.delete(materialDetails);
		addMessage(redirectAttributes, "删除物料明细管理成功");
		return "redirect:"+Global.getAdminPath()+"/ka/materialDetails/?repage";
	}
	
	@RequiresPermissions("ka:materialDetails:delete")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(MaterialDetails materialDetails, RedirectAttributes redirectAttributes) {
		materialDetailsService.deleteAll(materialDetails);
		addMessage(redirectAttributes, "删除货品管理成功");
		return "redirect:"+Global.getAdminPath()+"/ka/materialDetails/?repage";
	}

}